<?php
	use Carbon\Carbon;

	require __DIR__.'/bootstrap/autoload.php';
	require __DIR__.'./SqlsrvDao.php';
	require __DIR__.'./LogFile.php';

	header("Content-type: text/html; charset=utf-8"); 
	$dotenv = new Dotenv\Dotenv(__DIR__);
	$dotenv->load();

	try {		
   
        $db_host = getenv('DB_HOST');   
		$db_name = getenv('DB_DATABASE');
		$db_url = "sqlsrv:Server=".$db_host.";Database=".$db_name;   

        $db_user = getenv('DB_USERNAME');         
        $db_password = getenv('DB_PASSWORD');   

        $db = new PDO($db_url, $db_user, $db_password); 
	} catch (PDOException $e) {
		echo "Failed to get DB handle: " . $e->getMessage() . "\n";
		exit;
	}

	$dao = new SqlsrvDao($db);
	$mark='04';
	
	if(getenv('LOG_TYPE')=='Self_Production') {
		$mark='01';		
	}

	$lastDate = $dao->getLastDate($mark);
	$init_dt = '';
	
	if (!$lastDate) {
		$init_dt='2018-05-01 00:00:00';
	}else
	{
		$init_dt=$lastDate['filename'];
	}

	$nextDate =  Carbon::createFromFormat('Y-m-d H:i:s', $init_dt)->addDay();
	$toDay= Carbon::now()->toDateString();

	// echo $toDay;
	// exit;
	// dd($nextDate->format('Y-m-d'));
	// dd($dao->exist($nextDate->format('Y-m-d'), '01'));
	// exit;
	if (getenv('LOG_TYPE')=='Self_Production') {
		while ($nextDate<$toDay) {
			if (!$dao->exist($nextDate->format('Y-m-d'), '01')){
				$filename1 = getenv('LOG_PATH').$nextDate->format('Ymd').'CH01'.'.txt';
				$bc_log1 = LogFile::getInstance()->toSelfArray($filename1);	
				if ($bc_log1) {
					$dao->insertLog($bc_log1);
					$dao->insertLogwj($nextDate->format('Y-m-d'), '01');
				}
			}

			if (!$dao->exist($nextDate->format('Y-m-d'), '02')){
				$filename2 = getenv('LOG_PATH').$nextDate->format('Ymd').'CH02'.'.txt';
				$bc_log2 = LogFile::getInstance()->toSelfArray($filename2);	
				if ($bc_log2) {
					$dao->insertLog($bc_log2);
					$dao->insertLogwj($nextDate->format('Y-m-d'), '02');
				}
			}

			if (!$dao->exist($nextDate->format('Y-m-d'), '03')){
				$filename3 = getenv('LOG_PATH').$nextDate->format('Ymd').'CH03'.'.txt';
				$bc_log3 = LogFile::getInstance()->toSelfArray($filename3);	
				if ($bc_log3) {
					$dao->insertLog($bc_log3);
					$dao->insertLogwj($nextDate->format('Y-m-d'), '03');
				}
			}

			$nextDate = $nextDate->addDay();	
	    }
	} else {
		while ($nextDate<$toDay) {
			if (!$dao->exist($nextDate->format('Y-m-d'), '04')){
				$filename4 = getenv('LOG_PATH').substr($nextDate->format('Ymd'),2).'04'.'.txt';				
				$bc_log4 = LogFile::getInstance()->toOtherArray($filename4);	
				if ($bc_log4) {
					$dao->insertLog($bc_log4);
					$dao->insertLogwj($nextDate->format('Y-m-d'), '04');
				}
			}

			if (!$dao->exist($nextDate->format('Y-m-d'), '05')){
				$filename5 = getenv('LOG_PATH').substr($nextDate->format('Ymd'),2).'05'.'.txt';
				$bc_log5 = LogFile::getInstance()->toOtherArray($filename5);	
				if ($bc_log5) {
					$dao->insertLog($bc_log5);
					$dao->insertLogwj($nextDate->format('Y-m-d'), '05');
				}
			}

			if (!$dao->exist($nextDate->format('Y-m-d'), '06')){
				$filename6 = getenv('LOG_PATH').substr($nextDate->format('Ymd'),2).'06'.'.txt';
				$bc_log6 = LogFile::getInstance()->toOtherArray($filename6);	
				if ($bc_log6) {
					$dao->insertLog($bc_log6);
					$dao->insertLogwj($nextDate->format('Y-m-d'), '06');
				}
			}

			if (!$dao->exist($nextDate->format('Y-m-d'), '07')){
				$filename7 = getenv('LOG_PATH').substr($nextDate->format('Ymd'),2).'07'.'.txt';
				$bc_log7 = LogFile::getInstance()->toOtherArray($filename7);	
				if ($bc_log7) {
					$dao->insertLog($bc_log7);
					$dao->insertLogwj($nextDate->format('Y-m-d'), '07');
				}
			}
			
			if (!$dao->exist($nextDate->format('Y-m-d'), '08')){
				$filename8 = getenv('LOG_PATH').substr($nextDate->format('Ymd'),2).'08'.'.txt';
				$bc_log8 = LogFile::getInstance()->toOtherArray($filename8);	
				if ($bc_log8) {
					$dao->insertLog($bc_log8);
					$dao->insertLogwj($nextDate->format('Y-m-d'), '08');
				}
			}

			$nextDate = $nextDate->addDay();
		}
	}

    //print_r($bc_log);

	echo 'success';
